import Member from "@/common/model/Member"
function Demand(){
    this.id = null;
    this.title = "";
    this.member_id = "";
    this.member = new Member();
    this.demand_content = "";//内容
    this.demand_type = "";//需求类别
    this.demand_area = [];//地区
    this.demand_industry = [];// 所属行业
    this.demand_beginDate = ""; 
    this.formated_beginDate_time ="";//格式化时间
    this.demand_endDate ="";
    this.formated_endDate_time ="";
    this.item_type = "";
    this.demand_invest = null;
    this.demand_presentInvest = null;
    this.demand_needInvest = null;
    this.demand_use = "";//需求用途
    this.demand_operation = "";
    this.demand_diya = "";
    this.demand_master = "";
    this.demand_tele = "";
    this.demand_address = "";
    this.appendix_id = "";
    this.appendix = [];
    this.demand_remark = "";
    this.create_time = "";
    this.format_create_time = "";
    this.status = null;
    this.number = "";
    this.safety_grade = 0;
}
Demand.prototype.parse = function(data){
    if(!data) return;
    Object.assign(this, data, {
        number: data.number,
        member: Member.parse(data.member || {}),
        demand_area: data.demand_area ? data.demand_area.split(",") : [], // 区域
        demand_beginDate: data.demand_beginDate =="0000000000000"? '': new Date(parseInt(data.demand_beginDate)).toLocaleDateString(),
        demand_endDate: data.demand_endDate =="0000000000000"? '': new Date(parseInt(data.demand_endDate)).toLocaleDateString(),
        format_create_time:new Date(parseInt(data.create_time)).toLocaleDateString(),
        //+" "+new Date(parseInt(data.create_time)).getHours()+":"+new Date(parseInt(data.create_time)).getMinutes()
        demand_industry: data.demand_industry ? data.demand_industry.split(",") : []
    });
}
Demand.prototype.serialize = function(){
    return Object.assign({}, this, {
        demand_area: this.demand_area.join(','),
        demand_beginDate: Date.parse(this.demand_beginDate),
        demand_endDate: Date.parse(this.demand_endDate),
        appendix_id:this.appendix.map(function(elem){return elem.id}).join(","),
        demand_industry:this.demand_industry.join(',')
    });
}

// 暂时将属性存起来，做一次备份
Demand.prototype.storageValue = function() {
    this.storage = this.serialize();
}

// 用备份的数据覆盖现有数据
Demand.prototype.applyStorageValue = function() {
    if(this.storage) {
        this.parse(this.storage);
    }
}

// ******************** 类静态属性与方法 **********************************************
// 解析json数据，返回一个对象
Demand.parse = function(data) {
    var o = new Demand();
    o.parse(data);
    return o;
}

// 解析json数组，返回对象数组
Demand.parseList = function(list) {
    var len = list.length;
    var arr = new Array(len);
    for(var i = 0; i < len; i++) {
        arr[i] = Demand.parse(list[i]);
    }
    return arr;
}
export default Demand